#include <Arduino.h>
#include "EEPROM.h"
#include "eeprom_.h"
#define DATA_LEN (LEN1 * INT_LEN + LEN2 * STR_LEN)
unsigned char save_data[DATA_LEN];
int *p1 = (int *)(save_data);
char *p2 = (char *)(save_data + (LEN1 * INT_LEN));
void eeprom_clear()
{
    EEPROM.begin(4096);
    for (int i = 0; i < 4096; i++)
    {
        EEPROM.write(i,255);
    }
    EEPROM.commit();
    EEPROM.end();
}
void eeprom_read()
{
    EEPROM.begin(START + DATA_LEN);
    for (int i = 0; i < DATA_LEN; i++)
    {
        save_data[i] = EEPROM.read(START + i);
    }
    bool flag = true;
    for (int i = 1; i < DATA_LEN; i++)
    {
        if (save_data[i] != 255)
        {
            flag = false;
            break;
        }
    }
    // 如果未保存过值，则设置初始值
    if (flag)
    {
        // 设置默认值int
        p1[0] = 1;
        for (int i = 1; i < LEN1; i++)
        {
            p1[i] = 0;
        }
        // 设置默认值String
        const char *default_wifi = "11111111";
        const char *default_pass = "";
        const char *default_ip = "192.168.1.123";
        const char *default_gateway = "192.168.1.1";
        const char *default_subnet = "255.255.255.0";
        const char *default_dns = "114.114.114.114";
        const char *default_relay_io = "0";
        const char *default_led_io = "2";
        const char *default_computer_state_io = "12";
        const char *default_computer_rst_io = "13";
        const char *default_control_mode = "0";
        const char *default_ntp = "ntp1.aliyun.com";
        const char *default_blinker_auth = "";
        const char *default_blinker_enable = "close";
        const char *default_bemfa_auth = "";
        const char *default_bemfa_enable = "close";
        const char *default_values[] = {
            default_wifi, default_pass, default_ip, default_gateway,default_subnet, default_dns,
            default_relay_io, default_led_io,default_computer_state_io,
            default_computer_rst_io,default_control_mode,default_ntp, 
            default_blinker_auth, default_blinker_enable, default_bemfa_auth, default_bemfa_enable};
        for (int i = 0; i < LEN2; i++)
        {
            memcpy(p2 + (i * STR_LEN), default_values[i], strlen(default_values[i]) + 1);
        }
    }
    // 添加字符串结束符
    for (int i = 0; i < LEN2; i++)
    {
        (p2 + (i * STR_LEN) + STR_LEN - 1)[0] = '\0';
    }
    EEPROM.end();
}
void eeprom_write()
{
    EEPROM.begin(START + DATA_LEN);
    for (int i = 0; i < DATA_LEN; i++)
    {
        EEPROM.write(START + i, save_data[i]);
    }
    EEPROM.commit();
    EEPROM.end();
}
int eeprom_get_int(int index)
{
    int value = *(p1 + index);
    return value;
}
String eeprom_get_string(int index)
{
    String str = String(p2 + (index * STR_LEN));
    return String(str.c_str()); // 去多余字符
}
long eeprom_get_mode2_opening_second()
{
    return p1[MODE2_OPENING_HOUR] * 60 * 60 + p1[MODE2_OPENING_MINUTE] * 60 + p1[MODE2_OPENING_SECOND];
}
long eeprom_get_mode2_closeing_second()
{
    return p1[MODE2_CLOSING_HOUR] * 60 * 60 + p1[MODE2_CLOSING_MINUTE] * 60 + p1[MODE2_CLOSING_SECOND];
}
long eeprom_get_mode3_opening_second()
{
    return p1[MODE3_OPENING_HOUR] * 60 * 60 + p1[MODE3_OPENING_MINUTE] * 60 + p1[MODE3_OPENING_SECOND];
}
